package com.drinks.yiniang.adapter.user.order;

import android.content.Context;
import android.graphics.Typeface;
import android.support.annotation.Nullable;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.widget.Button;

import com.chad.library.adapter.base.BaseViewHolder;
import com.drinks.yiniang.R;
import com.drinks.yiniang.adapter.base.BaseMultiItemQuickRCVAdapter;
import com.drinks.yiniang.entity.user.order.MyOrderJson;
import com.drinks.yiniang.utils.GlideUtils;
import com.hansen.library.ui.widget.image.RatioRoundImageView;
import com.hansen.library.utils.ColorUtils;
import com.hansen.library.utils.StringUtils;

import java.util.List;

import static com.drinks.yiniang.entity.user.order.MyOrderJson.ITEM_TYPE_ORDER_BOTTOM;
import static com.drinks.yiniang.entity.user.order.MyOrderJson.ITEM_TYPE_ORDER_MIDDLE;
import static com.drinks.yiniang.entity.user.order.MyOrderJson.ITEM_TYPE_ORDER_TOP;
import static com.hansen.library.Constants.TYPE_ORDER_ALREADY_DELIVERED;
import static com.hansen.library.Constants.TYPE_ORDER_ALREADY_EVALUATE;
import static com.hansen.library.Constants.TYPE_ORDER_ALREADY_REFUND;
import static com.hansen.library.Constants.TYPE_ORDER_CANCEL;
import static com.hansen.library.Constants.TYPE_ORDER_PLATEFORM_CANCEL;
import static com.hansen.library.Constants.TYPE_ORDER_REFUNDIND;
import static com.hansen.library.Constants.TYPE_ORDER_REFUND_REFUSE;
import static com.hansen.library.Constants.TYPE_ORDER_SELFT_GET;
import static com.hansen.library.Constants.TYPE_ORDER_WAIT_FOR_DELIVER;
import static com.hansen.library.Constants.TYPE_ORDER_WAIT_FOR_EVALUATE;
import static com.hansen.library.Constants.TYPE_ORDER_WAIT_FOR_PAY;
import static com.hansen.library.Constants.TYPE_ORDER_WAIT_FOR_RECEIPT;

/**
 * 我的订单列表adapter
 *
 * @author han
 * @date 2019/8/30
 */
public class MyOrderAdapter extends BaseMultiItemQuickRCVAdapter<MyOrderJson.MyOrderList, BaseViewHolder> {

    private SpannableStringBuilder mBuilder;
    private int start, end;

    public MyOrderAdapter(Context mContext, @Nullable List<MyOrderJson.MyOrderList> data) {
        super(data);

        addItemType(ITEM_TYPE_ORDER_TOP, R.layout.item_order_top);
        addItemType(ITEM_TYPE_ORDER_MIDDLE, R.layout.item_order_middle);
        addItemType(ITEM_TYPE_ORDER_BOTTOM, R.layout.item_order_bottom);

        mBuilder = new SpannableStringBuilder();
    }

    @Override
    protected void convert(BaseViewHolder helper, MyOrderJson.MyOrderList item) {
        switch (helper.getItemViewType()) {
            case ITEM_TYPE_ORDER_TOP://头部
                //订单号
                mBuilder.clearSpans();
                mBuilder.clear();
                mBuilder.append(mContext.getString(R.string.text_order_no_colon));
                mBuilder.append(StringUtils.getNullEmptyConvert__(item.getOrder_no()));
                helper.setText(R.id.item_my_order_no, mBuilder);
                //订单状态
                helper.setText(R.id.item_my_order_status, getOrderStatusText(item.getStatus()));
                break;
            case ITEM_TYPE_ORDER_MIDDLE://中间
                if (item.getTempGoods() == null) {
                    item.setTempGoods(new MyOrderJson.MyOrderGoodsList());
                }

                //封面
                GlideUtils.loadSquareImage(mContext, (RatioRoundImageView) helper.getView(R.id.iv_order_goods_cover), item.getTempGoods().getCover_url());
                //商品名称
                helper.setText(R.id.tv_order_goods_name, item.getTempGoods().getGoods_name());
                //价格
                mBuilder.clearSpans();
                mBuilder.clear();
                mBuilder.append("¥");
                mBuilder.append(StringUtils.getNullEmptyConvert__(item.getTempGoods().getPay_price()));
                helper.setText(R.id.tv_order_goods_price, mBuilder);
                //推荐语
                helper.setText(R.id.tv_order_goods_slogan, item.getTempGoods().getRecommend_reason());
                //属性
                mBuilder.clearSpans();
                mBuilder.clear();
                mBuilder.append(StringUtils.judgeGreaterNumber(item.getTempGoods().getSingle_capacity(), 0f) ? item.getTempGoods().getSingle_capacity() : "");
                mBuilder.append(mBuilder.length() > 0 ? "ml" : "");
                helper.setText(R.id.tv_order_goods_attrs, mBuilder);
                //数量
                mBuilder.clearSpans();
                mBuilder.clear();
                mBuilder.append("x");
                mBuilder.append(StringUtils.getNullEmptyConvert__(item.getTempGoods().getNum()));
                helper.setText(R.id.tv_order_goods_num, mBuilder);
                break;
            case ITEM_TYPE_ORDER_BOTTOM://底部
                //订单统计
                mBuilder.clearSpans();
                mBuilder.clear();
                mBuilder.append("共");
                mBuilder.append(StringUtils.getNullEmptyConvert__(item.getGoods_num()));
                mBuilder.append("件商品 合计：¥");
                start = mBuilder.length();
                mBuilder.append(StringUtils.getNullEmptyConvert__(item.getPay_amount()));
                end = mBuilder.length();
                mBuilder.setSpan(new ForegroundColorSpan(ColorUtils.getColorById(mContext, R.color.color_282829)), start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                mBuilder.setSpan(new StyleSpan(Typeface.BOLD), start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                helper.setText(R.id.tv_my_order_total, mBuilder);

                setButtonStatus(helper, item.getStatus());
                break;
            default:
                break;
        }
    }

    /**
     * 获取订单状态文本
     *
     * @param status 0待支付 1已支付 2已发货 3已送达 4交易完成 5已评价 6已取消 7退款仲裁（退款中） 9门店自提 10已退款 11拒绝退款 12缺货取消一品脱订单状态
     * @return
     */
    private void setButtonStatus(BaseViewHolder helper, String status) {
        if (StringUtils.isEmpty(status)) {
            helper.setGone(R.id.btn_my_order_evaluate, false);
            helper.setGone(R.id.btn_my_order_delete, false);
            helper.setGone(R.id.btn_my_order_pay_or_sure, false);

            return;
        }

        switch (status) {
            case TYPE_ORDER_WAIT_FOR_PAY://待支付(显示取消订单, 支付)
                helper.setGone(R.id.btn_my_order_evaluate, false);
                helper.setGone(R.id.btn_my_order_delete, true);
                helper.setGone(R.id.btn_my_order_pay_or_sure, true);

                helper.setText(R.id.btn_my_order_delete, mContext.getString(R.string.text_order_cancel));
                helper.setText(R.id.btn_my_order_pay_or_sure, mContext.getString(R.string.text_pay));

                helper.addOnClickListener(R.id.btn_my_order_delete, R.id.btn_my_order_pay_or_sure);
                break;
            case TYPE_ORDER_WAIT_FOR_RECEIPT://待收货(显示确认收货)
            case TYPE_ORDER_ALREADY_DELIVERED://已送达
                helper.setGone(R.id.btn_my_order_evaluate, false);
                helper.setGone(R.id.btn_my_order_delete, false);
                helper.setGone(R.id.btn_my_order_pay_or_sure, true);

                helper.setText(R.id.btn_my_order_pay_or_sure, mContext.getString(R.string.text_receipt_confirm));

                helper.addOnClickListener(R.id.btn_my_order_pay_or_sure);
                break;
            case TYPE_ORDER_WAIT_FOR_EVALUATE://交易完成-待评价(显示评价)
                helper.setGone(R.id.btn_my_order_evaluate, true);
                helper.setGone(R.id.btn_my_order_delete, false);
                helper.setGone(R.id.btn_my_order_pay_or_sure, false);

                helper.setText(R.id.btn_my_order_evaluate, mContext.getString(R.string.text_evaluate));

                helper.addOnClickListener(R.id.btn_my_order_evaluate);
                break;
            case TYPE_ORDER_ALREADY_EVALUATE://已评价-交易成功(显示删除, 晒单)
                helper.setGone(R.id.btn_my_order_evaluate, true);
                helper.setGone(R.id.btn_my_order_delete, true);
                helper.setGone(R.id.btn_my_order_pay_or_sure, false);

                helper.setText(R.id.btn_my_order_evaluate, mContext.getString(R.string.text_order_share));
                helper.setText(R.id.btn_my_order_delete, mContext.getString(R.string.text_delete));

                helper.addOnClickListener(R.id.btn_my_order_delete, R.id.btn_my_order_evaluate);
                break;
            case TYPE_ORDER_CANCEL://已取消
            case TYPE_ORDER_ALREADY_REFUND://已退款
            case TYPE_ORDER_REFUND_REFUSE://拒绝退款
            case TYPE_ORDER_PLATEFORM_CANCEL://平台取消
                helper.setGone(R.id.btn_my_order_evaluate, false);
                helper.setGone(R.id.btn_my_order_delete, true);
                helper.setGone(R.id.btn_my_order_pay_or_sure, false);

                helper.setText(R.id.btn_my_order_delete, mContext.getString(R.string.text_delete));

                helper.addOnClickListener(R.id.btn_my_order_delete);
                break;
            case TYPE_ORDER_WAIT_FOR_DELIVER://已支付
            case TYPE_ORDER_REFUNDIND://退款中
            case TYPE_ORDER_SELFT_GET://门店自提
                helper.setGone(R.id.btn_my_order_evaluate, false);
                helper.setGone(R.id.btn_my_order_delete, false);
                helper.setGone(R.id.btn_my_order_pay_or_sure, false);
                break;
            default:
                break;
        }
    }

    /**
     * 获取订单状态文本
     *
     * @param status 0待支付 1已支付 2已发货 3已送达 4交易完成 5已评价 6已取消 7退款仲裁（退款中） 9门店自提 10已退款 11拒绝退款 12缺货取消一品脱订单状态
     * @return
     */
    private String getOrderStatusText(String status) {
        if (status == null) {
            return "";
        }

        switch (status) {
            case TYPE_ORDER_WAIT_FOR_PAY://待支付
                return mContext.getString(R.string.text_wait_for_pay);
            case TYPE_ORDER_WAIT_FOR_DELIVER://已支付
                return mContext.getString(R.string.text_wait_for_deliver);
            case TYPE_ORDER_WAIT_FOR_RECEIPT://待收货
                return mContext.getString(R.string.text_wait_for_receipt);
            case TYPE_ORDER_ALREADY_DELIVERED://交易完成
                return mContext.getString(R.string.text_already_delivered);
            case TYPE_ORDER_WAIT_FOR_EVALUATE://待评价
                return mContext.getString(R.string.text_wait_for_evaluate);
            case TYPE_ORDER_ALREADY_EVALUATE://已评价-交易成功
                return mContext.getString(R.string.text_trade_successful);
            case TYPE_ORDER_CANCEL://已取消
                return mContext.getString(R.string.text_already_cancel);
            case TYPE_ORDER_REFUNDIND://退款中
                return mContext.getString(R.string.text_refunding);
            case TYPE_ORDER_SELFT_GET://门店自提
                return mContext.getString(R.string.text_shop_yourself);
            case TYPE_ORDER_ALREADY_REFUND://已退款
                return mContext.getString(R.string.text_already_refund);
            case TYPE_ORDER_REFUND_REFUSE://拒绝退款
                return mContext.getString(R.string.text_refuse_refund);
            case TYPE_ORDER_PLATEFORM_CANCEL://平台取消
                return mContext.getString(R.string.text_already_cancel_by_platform);
            default:
                return "";
        }
    }
}
